﻿using System;
using System.Linq;
using System.Net;
using System.Threading;
using System.Windows.Forms;
using SuperSocket.ClientEngine;
using SuperSocketClient.AppBase;
using SuperSocketClient.Common;

namespace SuperSocketClient
{
    public partial class Form1 : Form
    {
        private EasyClient<MyPackageInfo> client;
       // private Timer timer1;

  /////////////////////////////////////////////////////////////////////////////

        #region 解决跨线程调用UI组件问题     
        private void DelegateAction(Action action)
        {
            if (InvokeRequired)
            {
                Invoke(action);
            }
            else
            {
                action();
            }
        }
        #endregion

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            var timer = new System.Timers.Timer(1000);
            timer.Elapsed += (s, x) =>
            {
                DelegateAction(() =>
                {  
                    txtAll.Text = LogHelper.SetOnLog();
                    txtAll.Select(txtAll.TextLength, 0);
                    txtAll.ScrollToCaret();
                });
            };
            timer.Enabled = true;
            timer.Start();
        }

        private void btnConn_Click(object sender, EventArgs e)
        {
            if (client == null || !client.IsConnected)
                ConnectServer();
        }
        /// <summary>
        /// 连接服务器
        /// </summary>
        private async void ConnectServer()
        {
            client = new EasyClient<MyPackageInfo>();
            client.Initialize(new MyReceiveFilter());
            client.Connected += OnClientConnected;
            client.NewPackageReceived += OnPagckageReceived;
            client.Error += OnClientError;
            client.Closed += OnClientClosed;

            var webSocketUrl = System.Configuration.ConfigurationManager.AppSettings["WebSocketURL"];
            var webSocketPort = System.Configuration.ConfigurationManager.AppSettings["WebSocketPort"];
            var connected =
                await client.ConnectAsync(new IPEndPoint(IPAddress.Parse(webSocketUrl), int.Parse(webSocketPort)));

           
          
        }

        private void OnClientClosed(object sender, EventArgs e)
        {
            int attmpts = 5;
            //if (timer1 != null)
            //{
            //    timer1.Dispose();
            //}
            do
            {
                LogHelper.WriteLog("连接已断开...");
                LogHelper.WriteLog("等待5秒中后重新连接");
                Thread.Sleep(5000);
                ConnectServer();
                attmpts--;
            } while (!client.IsConnected && attmpts > 0);


        }

        private void OnClientError(object sender, ErrorEventArgs e)
        {
            LogHelper.WriteLog("客户端错误：" + e.Exception.Message);
        }

        private void OnPagckageReceived(object sender, PackageEventArgs<MyPackageInfo> e)
        {
           LogHelper.WriteLog("收到文本下行"+e.Package.Body);

        }

        private void OnClientConnected(object sender, EventArgs e)
        {
            LogHelper.WriteLog("已连接到服务器...");
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            LogHelper.AllLines.Clear();
            LogHelper.AllLines.Add("清空了..");
            LogHelper.DisplayLength = 0;
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
          SendMessage(SingleBidDoc.Announce,txtMsg.Text);
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            SendMessage(SingleBidDoc.Login,txtMsg.Text);
           
        }
       

        private void btnDecrypt_Click(object sender, EventArgs e)
        {
            SendMessage(SingleBidDoc.DecSendTime, txtMsg.Text);
        }

        private void btnAnnounce_Click(object sender, EventArgs e)
        {
            SendMessage(SingleBidDoc.Announce, txtMsg.Text);
        }

        private void btnSignature_Click(object sender, EventArgs e)
        {
            SendMessage(SingleBidDoc.Signature, txtMsg.Text);
        }

        private void btnSwitchPanel_Click(object sender, EventArgs e)
        {
            SendMessage(SingleBidDoc.Dissent, txtMsg.Text);
        }

        /// <summary>
        /// 发送命令和消息到服务器
        /// </summary>
        /// <param name="command"></param>
        /// <param name="message"></param>
        private void SendMessage(SingleBidDoc command, string message)
        {
            if (client == null || !client.IsConnected || message.Length <= 0) return;
            var response = BitConverter.GetBytes((ushort)command).Reverse().ToList();
            var arr = System.Text.Encoding.UTF8.GetBytes(message);
            response.AddRange(BitConverter.GetBytes((ushort)arr.Length).Reverse().ToArray());
            response.AddRange(arr);
            client.Send(response.ToArray());

            LogHelper.WriteLog($"发送{command.GetDescription()}数据：" + txtMsg.Text);
           
            DelegateAction(() =>
            {
                txtMsg.Text = string.Empty;
            });
        }
    }
}
